package pw.goyd.game.server;

import pw.goyd.db.RedisUtil;
import pw.goyd.game.manager.RoomManager;
import pw.goyd.subscribe.PlayerCustomer;

import java.util.HashMap;
import java.util.Map;

/**
 * cache用户列
 *
 * @author StanWind
 */
public class LoginList {
    //accountid----player
    private static Map<String, PlayerCustomer> loginList;

    static {
        loginList = new HashMap<>();
    }

    /**
     * 添加一个在线Player
     *
     * @param account 账号
     * @param player  玩家
     * @return 如果账号存在 返回假
     */
    public static boolean addPlayer(String account, PlayerCustomer player) {
        //   PlayerCustomer s = loginList.get(account);
        //  if (s != null)
        //    return false;
        Log.getLogger().info("add---->" + account);
        loginList.put(account, player);

        return true;
    }

    /**
     * get Player by account
     *
     * @param account 账号
     * @return 玩家
     */
    public static PlayerCustomer getPlayer(String account) {
        return loginList.get(account);
    }


    /**
     * get Player by channel id
     *
     * @param channelID channelID
     * @return player
     */
    public static PlayerCustomer getPlayer(long channelID) {
        String account = null;
        PlayerCustomer s = null;
        for (String key : loginList.keySet()) {
            s = loginList.get(key);
            if (s.getChannelID() == channelID) {
                account = key;
                break;
            }
        }

        return account != null ? s : null;
    }

    /**
     * remove by player
     *
     * @param player playercustomer
     */
    public static void removePlayer(PlayerCustomer player) {
        if (player != null) {
            RoomManager.leaveRoom(player);
            player.unsub();
            Log.getLogger().debug("订阅---->回收jedis " + player.getAccount_id());
            if (player.getJedis() != null)
                RedisUtil.returnResource(player.getJedis());

            removePlayer(player.getAccount_id());
        }

    }

    /**
     * remove player by channel id
     *
     * @param channelID playercustomer
     */
    public static void removePlayer(long channelID) {
        String account;
        PlayerCustomer s = getPlayer(channelID);

        if (s != null) {
            account = s.getAccount_id();
            RoomManager.leaveRoom(s);
            s.unsub();

            Log.getLogger().debug(
                    "订阅---->回收jedis [" + s.getAccount_id() + "]");
            RedisUtil.returnResource(s.getJedis());

            Log.getLogger().info("LoginList---->移除账号[" + account + "]");
            removePlayer(account);
        }
    }

    /**
     * remove player
     *
     * @param account 账号
     */
    public static void removePlayer(String account) {
        Log.getLogger().debug("remove[" + account + "]" + more());
        //loginList.put(account,null);
        loginList.remove(account);
    }

    /**
     * 账号是否在线
     *
     * @param account 账号
     * @return 是否在线
     */
    public static boolean isAccountExist(String account) {
        PlayerCustomer s = loginList.get(account);
//        if (s != null) {
//            return s.getChannel().isOpen(); //  如果连接断开就表示不存在
//        } else {
//            return false;
//        }
        return s != null && s.getChannel().isOpen();
    }

    /**
     * 遍历list信息
     *
     * @return 玩家列表信息
     */
    public static String more() {
        StringBuilder sb = new StringBuilder();
        sb.append("当前玩家:" + loginList.size() + "\n");
        for (String key : loginList.keySet()) {
            PlayerCustomer p = loginList.get(key);
            sb.append(p + "\n");
        }
        return sb.toString();
    }


}
